<template>
  <div>
    <el-tree ref="tree" :data="permissionTreeData" :props="props" show-checkbox node-key="permissionId" @check-change="checkChange" default-expand-all>
    </el-tree>
    <!-- <el-tree ref="tree" :data="permissionTreeData" :props="props" :default-checked-keys="permissionIds" show-checkbox node-key="permissionId" @check-change="checkChange" default-expand-all>
    </el-tree> -->
  </div>
</template>

<script>
import { CONSTANT_OBJ } from '@/utils/constant'
import api from '../api'

export default {
  props: { roleId: String },
  data () {
    return {
      props: {
        label: 'permissionName',
        children: 'childPermissionTree',
        isLeaf: 'isLeaf'
      },
      permissionTreeData: [],
      permissionIds: [],
      tempIds: [],
      systemPermissionVos: {},
      isSelectMenuId: [], // 用户所选子节点
      isSelectMenuHalfId: [] // 用户所选半选节点
    }
  },
  mounted () {
    this.getAllPermission(this.getPermission)
  },
  methods: {
    // 获取角色权限
    getPermission () {
      api.getRolePermission(this.roleId).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          let systemPermissionVos = {}
          if (data.systemPermissionVos) {
            systemPermissionVos = data.systemPermissionVos.find(v => v.sysCode === 'AGENT') || {}
          } else {
            systemPermissionVos = {}
          }
          this.systemPermissionVos = systemPermissionVos
          // this.permissionIds = systemPermissionVos.permissionIds || []
          this.$nextTick(() => {
            setTimeout(() => {
              this.disposeNodeCheck(systemPermissionVos.permissionIds)
            }, 0)
          })
        }
      })
    },
    // 获取全部权限
    getAllPermission (callback) {
      api.getAllRolePermission('AGENT').then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.permissionTreeData = [...data]
          console.log(data, '全部权限')
          callback()
        }
      })
    },
    disposeNodeCheck (list) {
      console.log(list, '角色权限')
      if (!list) return
      list.forEach(ele => {
        let node = this.$refs['tree'].getNode(ele)
        if (node && node.isLeaf) {
          this.$refs['tree'].setChecked(node, true)
        }
      })
    },
    checkChange (data, checked, indeterminate) {
      // this.permissionIds = this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys())
      this.tempIds = this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys())
    },
    // 提交
    submit () {
      const { roleId, tempIds } = this
      const systemPermissionVos = [{ ...this.systemPermissionVos }]
      systemPermissionVos[0].permissionIds = tempIds
      console.log(tempIds, '权限id列表')
      api.postChangeSystemPermissionsData({
        roleId,
        systemPermissionVos
      }).then(res => {
        const { responseCode, responseMsg } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message: `${responseMsg}`,
            type: 'success'
          })
          this.$emit('uppdataSuccess')
        }
      })
    }
  }
}
</script>
